<template>
  <div>
    <div class="box">
      <img src="../../assets/image/pic.jpg" />
      <span>账号登录</span>
      <van-cell-group style="border:0">
        <van-row>
          <van-field v-model="username" placeholder="邮箱/手机号码/小米ID" @blur="userValid" />
        </van-row>
      </van-cell-group>
      <van-cell-group>
        <van-row>
          <van-field
            v-model="password"
            type="password"
            placeholder="请输入密码"
            right-icon="eye"
            @blur="passwordValid"
          />
        </van-row>
      </van-cell-group>
      <van-button color="#FF6700" size="large" @click="goLogin">登录</van-button>
      <van-button class="zhuce" color="#ccc" size="large" @click="goRegister">手机短信登录/注册</van-button>
      <div>
        <span>
          <a href="javascript::">立即注册 |</a>
        </span>
        <span>
          <a href="javascript::">忘记密码?</a>
        </span>
      </div>
      <van-divider :style="{ color: '#ddd', borderColor: '#ddd', padding: '0 6px' }">其他方式登录</van-divider>
      <div class="loginMethods">
        <a href="javascript::">
          <img class="pic" src="../../assets/image/weibo.jpg" alt />
        </a>
        <a href="javascript::">
          <img src="../../assets/image/zfb.jpg" />
        </a>
        <a href="javascript::">
          <img src="../../assets/image/weixin.jpg" />
        </a>
      </div>
    </div>
  </div>
</template>
<script>
import { Toast } from 'vant'

export default {
  data () {
    return {
      username: 'admin',
      password: '123456'
    }
  },
  created () {},
  methods: {
    userValid () {
      if (!/^[a-zA-Z0-9_-]{5,16}$/.test(this.username)) {
        Toast('请输入6-16位用户名')
        return false
      }
      return true
    },
    passwordValid () {
      if (!/^(\w){6,20}$/.test(this.password)) {
        Toast('请输入6-20个字母、数字、下划线')
        return false
      }
      return true
    },
    goLogin () {
      if (this.userValid() && this.passwordValid()) {
        this.$http.get('/login', {
          params: {
            username: this.username,
            password: this.password
          }
        }).then(res => {
          window.localStorage.setItem('token', 'ok')
          this.$router.push('home')
        }).catch(res => {
          Toast('用户名或密码不对')
        })
      } else if (this.userValid()) {
        Toast('请输入密码')
      } else {
        Toast('请输入正确的用户名')
      }
    },
    goRegister () {
      this.$router.push('register')
    }
  }
}
</script>
<style lang="less" scoped>
* {
  margin: 0;
  padding: 0;
}
.box {
  display: flex;
  flex-direction: column;
  align-items: center;
  width: 80%;
  margin: 65px auto;
  // background-color: pink;
  img {
    width: 60px;
    height: 60px;
    vertical-align: middle;
    border-radius: 10px;
  }
  span {
    font-size: 20px;
    margin: 20px 0;
  }
  van-field {
    margin-bottom: 20px;
    border-bottom: 2px solid #ccc;
  }
  .van-hairline--top-bottom {
    position: static;
  }
  .van-cell-group {
    width: 100%;
    .van-row {
      border-bottom: 2px solid #ccc;
    }
  }
  .van-cell {
    padding-left: 0;
  }

  .van-button {
    margin-top: 20px;
    border-radius: 8px;
    font-size: 16px;
  }

  .zhuce {
    color: #000 !important;
    border: 1px solid #ccc;
    background-color: #fff !important;
  }
  a {
    font-size: 14px;
    color: #ccc;
  }
  .van-divider {
    width: 100%;
  }
  .loginMethods {
    a {
      margin: 10px;
      img {
        width: 40px;
        height: 40px;
        border-radius: 50%;
      }
    }
  }
}
</style>
